package com.ningmengs789.live.ui.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ningmengs789.live.ActivityUtils;
import com.ningmengs789.live.BuildConfig;
import com.ningmengs789.live.R;
import com.ningmengs789.live.http.OKHttpUtils;
import com.ningmengs789.live.Interface.RequestCallback;
import com.ningmengs789.live.live.live.common.utils.DialogUitl;
import com.ningmengs789.live.net.Config;
import com.ningmengs789.live.net.broadcast.BroadcastManager;
import com.ningmengs789.live.net.network.http.HttpException;
import com.ningmengs789.live.net.response.LoginResponse;
import com.ningmengs789.live.net.utils.json.JsonMananger;
import com.ningmengs789.live.server.widget.LoadDialog;
import com.tencent.imsdk.TIMCallBack;
import com.tencent.imsdk.TIMManager;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;


public class SettingActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.tv_title_center)
    TextView tv_title_center;

    @BindView(R.id.phoneLayout)
    RelativeLayout phoneLayout;

    @BindView(R.id.locationLayout)
    RelativeLayout locationLayout;

    @BindView(R.id.wxLayout)
    RelativeLayout wxLayout;

    @BindView(R.id.qqLayout)
    RelativeLayout qqLayout;

    @BindView(R.id.aboutUs)
    RelativeLayout aboutUs;
    @BindView(R.id.onLineService)
    RelativeLayout onLineService;
    @BindView(R.id.ac_set_exit)
    RelativeLayout mExit;

    @BindView(R.id.phoneNum)
    TextView phoneNum;
    @BindView(R.id.wxBind)
    TextView wxBind;
    @BindView(R.id.qqBind)
    TextView qqBind;

    @BindView(R.id.versionName)
    TextView versionName;

    private LoginResponse mResponse;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        setHeadLayout();
        initView();
        initEvent();
    }

    private void initView() {
        tv_title_center.setVisibility(View.VISIBLE);
        tv_title_center.setText(R.string.set_set);
    }

    private void initEvent() {
        phoneLayout.setOnClickListener(this);
        locationLayout.setOnClickListener(this);
        wxLayout.setOnClickListener(this);
        qqLayout.setOnClickListener(this);

        aboutUs.setOnClickListener(this);
        onLineService.setOnClickListener(this);
        mExit.setOnClickListener(this);

        versionName.setText(BuildConfig.VERSION_NAME);

        if (ActivityUtils.IS_VIDEO()){
            mExit.setVisibility(View.GONE);
            aboutUs.setVisibility(View.GONE);
            onLineService.setVisibility(View.GONE);
            locationLayout.setVisibility(View.GONE);
        }

        OKHttpUtils.getInstance().getRequest("app/user/getBindingInfo", "", new RequestCallback() {
            @Override
            public void onError(int errCode, String errInfo) {
            }
            @Override
            public void onSuccess(String result) {
                if (result == null){
                    return;
                }
                try {
                    JSONObject data = new JSONObject(result);
                    phoneNum.setText(data.getString("binding_mobile"));
//                    phoneNum.setTextColor(getResources().getColor(R.color.color_333333));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

        getUserInfo();
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        if (mResponse == null) {
            return;
        }
        switch (view.getId()) {
            case R.id.phoneLayout:
                intent.setClass(this, BindPhoneActivity.class);
                startActivity(intent);
                break;
            case R.id.locationLayout:
                intent.setClass(this, BindQQWXActivity.class);
                intent.putExtra("USER_INFO", mResponse);
                intent.putExtra("loginType", "location");
                startActivityForResult(intent, requestCode_);
                break;
            case R.id.wxLayout:
                intent.setClass(this, BindQQWXActivity.class);
                intent.putExtra("USER_INFO", mResponse);
                intent.putExtra("loginType", "weixin");
                startActivityForResult(intent, requestCode_);
                break;
            case R.id.qqLayout:
                intent.setClass(this, BindQQWXActivity.class);
                intent.putExtra("USER_INFO", mResponse);
                intent.putExtra("loginType", "qq");
                startActivityForResult(intent, requestCode_);
                break;
            case R.id.onLineService:
                intent.setClass(this, OnlineServiceActivity.class);
                startActivity(intent);
                break;
            case R.id.aboutUs:
                intent.setClass(this, AboutUsActivity.class);
                startActivity(intent);
                break;
            case R.id.ac_set_exit:
                DialogUitl.showSimpleHintDialog(mContext, getString(R.string.prompt), getString(R.string.quit), getString(R.string.cancel),
                        getString(R.string.setting_logout), true, true,
                        new DialogUitl.SimpleCallback2() {
                            @Override
                            public void onCancelClick() {

                            }
                            @Override
                            public void onConfirmClick(Dialog dialog, String content) {
                                dialog.dismiss();
                                LoadDialog.show(SettingActivity.this);

                                TIMManager.getInstance().logout(new TIMCallBack() {
                                    @Override
                                    public void onError(int code, String desc) {
                                        LoadDialog.dismiss(SettingActivity.this);
                                        BroadcastManager.getInstance(mContext).sendBroadcast(Config.LOGIN, Config.LOGOUTSUCCESS);
                                        finish();
                                    }
                                    @Override
                                    public void onSuccess() {
                                        LoadDialog.dismiss(SettingActivity.this);
                                        BroadcastManager.getInstance(mContext).sendBroadcast(Config.LOGIN, Config.LOGOUTSUCCESS);
                                        finish();
                                    }
                                });
                            }
                        });
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private static int requestCode_ = 1202;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == requestCode_){
            getUserInfo();
        }
    }

    private void getUserInfo(){
        OKHttpUtils.getInstance().getRequest("app/user/getUserInfo", "", new RequestCallback() {
            @Override
            public void onError(int errCode, String errInfo) {
            }
            @Override
            public void onSuccess(String result) {
                if (result == null){
                    return;
                }
                try {
                    mResponse = JsonMananger.jsonToBean(result, LoginResponse.class);
                    if (!mResponse.getMobile().equals("") && mResponse.getMobile() != null) {
                        phoneNum.setText(mResponse.getMobile());
//                        phoneNum.setTextColor(getResources().getColor(R.color.color_333333));
                        phoneLayout.setClickable(false);
                    }
                    if (!mResponse.getWeixin().equals("") && mResponse.getWeixin() != null) {
                        wxBind.setText(mResponse.getWeixin());
//                        wxBind.setTextColor(getResources().getColor(R.color.color_333333));
                        //wxLayout.setClickable(false);
                    }
                    if (!mResponse.getQq().equals("") && mResponse.getQq() != null) {
                        qqBind.setText(mResponse.getQq());
//                        qqBind.setTextColor(getResources().getColor(R.color.color_333333));
                        //qqLayout.setClickable(false);
                    }
                } catch (HttpException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
